// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import Userdata from "./user/userdata";

const {ccclass, property} = cc._decorator;

@ccclass
export default class Gamemain extends cc.Component {

    @property(cc.Label)
    countLabel:cc.Label = null;
    @property(cc.Animation)
    ganNode:cc.Animation = null;
    @property(cc.Node)
    muyu:cc.Node = null;
    @property(cc.AudioSource)
    soundM:cc.AudioSource =null;
    @property(cc.AudioSource)
    musicM:cc.AudioSource =null;

    onLoad () {
        this.node.on("click",this.meritsOnclick,this)
        this.InitRecord();
        this.musicM.play();

    }

    toggleClick(target){
        if (target.isChecked) {
            this.musicM.pause();
        }else{
            this.musicM.resume();
        }
    }

    InitRecord(){
        let value = cc.sys.localStorage.getItem("meritsCount")
        value = parseInt(value)
        Userdata.meritsCount = value?value:0;
    }

    meritsOnclick(){
        let unit = 1;
        Userdata.meritsCount += unit;
        cc.sys.localStorage.setItem("meritsCount", Userdata.meritsCount);

        this.soundM.play();

        cc.tween(this.muyu).delay(0.05).to(0.05,{scale:0.99}).to(0.05,{scale:1}).start();


        let t_Node = new cc.Node()
        t_Node.y = 100;
        let newLabel = t_Node.addComponent(cc.Label);
        newLabel.string = "功德 +" + unit
        this.node.addChild(t_Node)
        let distance = 150
        let time = distance/200
        cc.tween(t_Node).to(time,{opacity:0}).start();
        cc.tween(t_Node).by(time,{y:distance}).call(()=>{
            t_Node.destroy();
        }).start();

        this.ganNode.play();
    }

    update (dt) {
        this.countLabel.string = "当前功德：" + Userdata.meritsCount 
    }
}
